package com.mrjy.queries.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.dto.MyMessagesDto;
import com.mrjy.common.entity.QuestionGoodInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.queries.bo.IQuestionGoodInfoBo;
import com.mrjy.queries.dto.QuestionGoodInfoDTO;

@Controller
@RequestMapping("/queries/questionGood")
public class QuestionGoodInfoController {

	@Resource
	private IQuestionGoodInfoBo qGoodInfoBo;
	/**
	 * TODO 没有进行事务处理
	 * 我的问答--点赞
	 * @param questionGoodInfo questionGoodInfo.questionId(问题id)
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/addGood")
	@ResponseBody
	public String addGood(QuestionGoodInfo questionGoodInfo,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return qGoodInfoBo.addGood(questionGoodInfo, userInfo);
	}
	
	/**
	 * 点赞
	 * @param questionGoodInfo
	 * @param session
	 * @return
	 * @author Javin :isUer = true
	 */
	@RequestMapping("/insertGood")
	@ResponseBody
	public Object insertGood(QuestionGoodInfo questionGoodInfo,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户信息不能为空");
			return map;
		}
		questionGoodInfo.setUserId(userInfo.getId());
		return qGoodInfoBo.txInsertGood(questionGoodInfo, map);
	}
	
	/**
	 * (根据问题ID or 这评论ID) and user_id查找是否已经点过赞
	 * @param info
	 * @param session
	 * @return
	 * @author Javin :isUer = true
	 */
	@RequestMapping("/quesyExist")
	@ResponseBody
	public Object quesyExist(QuestionGoodInfo info,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户信息不能为空");
			return map;
		}
		info.setUserId(userInfo.getId());
		return qGoodInfoBo.quesyExist(info, map);
	}
	
	/**
	 * 根据Id更新未读状态为已读
	 * @param id
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/updateReadFlagToReaded")
	@ResponseBody
	public Object updateReadFlagToReaded(Integer id){
		return qGoodInfoBo.txUpdateReadFlagToReaded(id);
	}
	/**
	 * 根据Id查询用户的被点赞信息
	 * @param id
	 * @return
	 * @author hfei
	 */
	@RequestMapping("/queryMyGoodPageByCommentedId")
	@ResponseBody
	public Object queryMyGoodPageByCommentedId(MyMessagesDto dto){
		return qGoodInfoBo.queryMyGoodPageByCommentedId(dto);
	}
	/**
	 * 根据Id查询用户的被点赞排名信息
	 * @param id
	 * @return
	 * @author hfei
	 */
	@RequestMapping("/queryMyPraise")
	@ResponseBody
	public Object queryMyPraise(QuestionGoodInfoDTO dto){
		return qGoodInfoBo.queryMyPraise(dto);
	}
	
}
